package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.ToolFile;
import com.ruoyi.system.domain.vo.FileVo;
import com.ruoyi.system.mapper.ToolFileMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IToolFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


@Service
public class ToolFileServiceImpl implements IToolFileService {
    @Autowired
    private ToolFileMapper toolFileMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private RemoteFileService remoteFileService;

    public ToolFileServiceImpl() {
    }

    public List<ToolFile> selectToolFileList(ToolFile file) {
        return this.toolFileMapper.selectToolFileList(file);
    }

    public ToolFile selectToolFileByFileId(String fileId) {
        return this.toolFileMapper.selectToolFileByFileId(fileId);
    }

    public int insertToolFile(FileVo file) {
        ToolFile fileItem = new ToolFile();
        fileItem.setFileName(file.getFileName());
        file.setFileId(IdUtils.simpleUUID());
        file.setAddTime(new Date());
        SysUser sysUser = this.sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        file.setAddUser(sysUser.getNickName());
        return this.toolFileMapper.insertToolFile(file);
    }

    public int updateToolFile(ToolFile file) {
        return this.toolFileMapper.updateToolFile(file);
    }

    public JSONObject deleteToolFileByIds(String ids) {
        String[] idArr = ids.split(",");
        boolean a = false;
        StringBuilder filePaths = new StringBuilder();
        String[] var5 = idArr;
        int var6 = idArr.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String fileId = var5[var7];
            ToolFile file = this.toolFileMapper.selectToolFileByFileId(fileId);
            String filePath = file.getFileUrl();
            filePaths.append(",").append(filePath);
            this.toolFileMapper.deleteToolFileById(fileId);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("filePath", filePaths.substring(1));
        return jsonObject;
    }

    public int deleteToolFileById(String id) {
        ToolFile file = this.toolFileMapper.selectToolFileByFileId(id);
        boolean result = this.remoteFileService.deleteFile(file.getFileUrl());
        if (!result) {
            System.out.println("文件删除失败");
        }

        return this.toolFileMapper.deleteToolFileById(id);
    }

    public String tOOLFileUpload(List<FileVo> files) {
        HashMap<String, Object> reMap = new HashMap();

        try {
            reMap.put("aaaaa", "文件不为空");
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Iterator var4 = files.iterator();

            while(var4.hasNext()) {
                FileVo toolFile = (FileVo)var4.next();
                toolFile.setAddUser(loginUser.getSysUser().getNickName());
                this.insertToolFile(toolFile);
            }
        } catch (Exception var6) {
            var6.printStackTrace();
            reMap.put("code", 500);
            reMap.put("msg", "失败，文件已存在");
            return JSONObject.toJSONString(reMap);
        }

        reMap.put("code", 200);
        reMap.put("msg", "成功");
        return JSONObject.toJSONString(reMap);
    }
}
